home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6094 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: fc.hp.com!news
  2. From: Rick Wells <rwells>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: changing strings via pointers
  5. Date: 22 Feb 1996 17:25:56 GMT
  6. Organization: Hewlett-Packard Fort Collins Site
  7. Message-ID: <4gi8v4$alk@fcnews.fc.hp.com>
  8. References: <1996Feb22.125436.25503@leeds.ac.uk>
  9. NNTP-Posting-Host: blkbear.fc.hp.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.07 9000/715)
  14. X-URL: news:1996Feb22.125436.25503@leeds.ac.uk
  15.  
  16. csyamc@scs.leeds.ac.uk (A M Casey) wrote:
  17. >I reading in strings from a file using fgets, which stores the "\n" in
  18. >the array of chars which I have declared using a pointer.
  19. >
  20. >The thing is, I want to get rid of the "\n" and replace it with "\0", but
  21. >obviously I can only do this using the pointer. I'm using the following code:
  22. >
  23. >char *Contents < pointer to string
  24. >
  25. >int count = 0;
  26. >
  27. >
  28. >while (*(Contents + count)!= NULL)  /* the end of string is not reached */
  29. >{if (*(Contents + count) == atoi("\n"))
  30. >/* change the \n to a \0 and set the next char along to null */
  31. >{
  32. >*(Contents + count) = atoi("\0");
  33. >
  34. >*(Contents + count + 1) = NULL;
  35. >
  36. >
  37. >} 
  38. >else
  39. >/* read in the next char */
  40. >{
  41. >count++;
  42. >
  43. >I think the problem maybe my way of using the pointer - I can print out each
  44. >char as I get to it, but it doesnt seem to change the array at all.
  45. >Should I be using atoi?, it doesnt seem to work without it.
  46. >
  47. >Cheers
  48. >
  49. >ANdy
  50. >
  51.  
  52. I don't know what you are trying to do with the function "atoi". What it
  53. does is convert an ascii string to an integer. The result of atoi("\n")
  54. will always be zero if I'm not mistaken because there are no digits in
  55. the string to be converted. Your "while" prior to the "if" assures
  56. you will never get to the "if" when you are pointing at a zero. Your "if"
  57. will never succeed while you are in the loop and you will never change the
  58. "\n" to a "\0".
  59.  
  60. In any case, your program isn't doing anything you think it is. Here's a
  61. solution to removing the trailing '\n' from your string following a fgets().
  62.  
  63. FILE *f;
  64. char s[255]; /* very large string for the read */
  65. int  string_length;
  66.  
  67. f = fopen ( ... ); /* open your file */
  68. /* ... do some error checking which I left out */
  69.  
  70. fgets (s, 254, f);
  71. /* ... do some error checking which I left out */
  72.  
  73. /* remove the '\n' if there is one at the end (there probably is) */
  74.  
  75. string_length = strlen (s); /* use runtime library string routine */
  76. if (string_length > 0 && s[string_length-1] == '\n')
  77.     s[string_length-1] = '\0'; /* notice the single quotes ('). You had
  78.                     double quotes (") in your example */
  79.  
  80.     .
  81.     .
  82.     .
  83.  
  84. Hope this helps, Rick
  85.  
  86.